const express = require("express");
const app = express();
const appConfig = require("./config/config");

// 中间件
const session = require("express-session");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const cors = require("cors");

// 绑定全局模块变量
app.locals.adminPath = appConfig.adminPath;

const api = require("./routes/api");
const admin = require("./routes/admin");
const index = require("./routes/index");

// 配置中间件
middlewareConfiguration();

app.use("/", index);
app.use(`/${appConfig.adminPath}`, admin);
app.use("/api", api);

app.listen(3000);
console.log("http://127.0.0.1:3000");

function middlewareConfiguration() {
    // 配置静态资源
    app.use(express.static("static"));

    // express-session 配置
    app.use(
        session({
            secret: "dqf37",
            name: "session_id",
            resave: false,
            saveUninitialized: true,
            cookie: {
                // maxAge: 1000 * 60 * 30,
                secure: false,
            },
            rolling: true,
        })
    );

    // body-parser 配置
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(bodyParser.json());

    // 配置ejs模板引擎
    app.engine("html", ejs.__express);
    app.set("view engine", "html");

    // 跨域开启
    app.use(cors());
}